package com.mengwen.backstage.controller.admin;

import com.mengwen.backstage.controller.partner.NoticeController;
import com.mengwen.backstage.pojo.*;
import com.mengwen.backstage.service.AnnoService;
import com.mengwen.backstage.service.GetNotificationService;
import com.mengwen.backstage.service.NotificationService;
import com.mengwen.backstage.util.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author LiangYonghui
 * @date 2020/4/25 23:11
 * @description  管理员发布公告
 */
@RestController
@Slf4j
public class AnnouncementController {


    private static final Log LOG = LogFactory.getLog(NoticeController.class);

    @Autowired
    NotificationService notificationService;
    @Autowired
    GetNotificationService getNotificationService;

    @Autowired
    AnnoService annoService;


    /**
     * 发送通知
     * 将数据存储到通知表中
     *
     * @param sendAnnoDto
     * @return
     */
    @ResponseBody
    @PostMapping("/admin/send")
    public Result sendAnno(@RequestBody SendAnnoDto sendAnnoDto, HttpServletRequest request, Model model) {
        Claims claims = JwtUtil.checkJWT(sendAnnoDto.getToken());
        String role = (String) claims.get("role");
        String unionid = (String) claims.get("adminUnionid");
        String openid = (String) claims.get("adminOpenid");
        String partnerNickname = (String) claims.get("adminNickname");
        log.info("=====================发送者信息===========================");
        System.out.println(unionid + openid + partnerNickname);
        if ("admin".equals(role) && (null != partnerNickname) && (null != unionid) && (null != openid)) {
            // 执行注入表的操作
            Anno anno = new Anno();
            anno.setContent(sendAnnoDto.getContent());
            anno.setNickname(partnerNickname);
            anno.setDate(new Date());
            anno.setUnionid(unionid);
            anno.setOpenid(openid);

            int delete = annoService.deleteAnno();

            if (delete < 0) {
                log.info("删除旧的公告失败。");
            }

            int add = annoService.sendAnno(anno);

            if (add < 0) {
                log.info(partnerNickname + "插入公告失败，unionid：" + unionid + ",openid:" + openid);
                return new Result(false, StatusCode.ERROR, "发送失败，请联系开发者");
            }
            log.info(partnerNickname + ",发送通知成功，unionid：" + unionid + ",openid:" + openid);
            return new Result(true, StatusCode.OK, "发送成功");
        } else {
            return new Result(false, StatusCode.ERROR, "发送失败，请正确登录");
        }
    }


    /**
     * 获取公告
     * @return
     */
    @ResponseBody
    @RequestMapping("admin/getanno")
    public Anno getAnno(){
        Anno anno= annoService.getAnno();

        if (anno == null) { // 无新通知
            log.info("通知内容条数为空。");
            return null;
        }
        return anno;
}



}